//
//  MYTabBar.swift
//  sinaWeibo
//
//  Created by 于 on 16/8/8.
//  Copyright © 2016年 于. All rights reserved.
//

import UIKit

class MYTabBar: UITabBar {
    
    
     // MARK: 闭包
     var composeBtn: (() ->())?
    
    // MARK: 懒加载加号按钮
    
    private lazy var btn: UIButton = {
    
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "show", forControlEvents: .TouchUpInside)
        //设置背景图
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:
            .Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:
            .Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        //自适应大小
        button.sizeToFit()
        return button
    
    }()

    // MARK: 点击事件
    func show () ->() {
    
    composeBtn?()
    }
    
     // MARK: 初始化
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(btn)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 // MARK:自动布局
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        btn.center.x = frame.size.width * 0.5
        btn.center.y = frame.size.height * 0.5
        
        let itemWidth = frame.size.width / 5
        var index = 0
        
        for value in subviews {
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
            value.frame.size.width = itemWidth
                
            value.frame.origin.x = CGFloat(index) * itemWidth
            index++
                
                if index == 2 {
                
                index++
                }
                
            }
       
        }
        
    }

}
